package com.zhiyou.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.mapper.TeacherMapper;
import com.zhiyou.model.FyTeacher;
import com.zhiyou.service.TeacherService;
import com.zhiyou.utils.FTPUtil;

@Service
public class TeacherServiceImp implements TeacherService {
	
	@Autowired
	TeacherMapper mapper;
	
	@Autowired
	AmqpTemplate amqpTemplate;
	
	@Autowired
	RedisTemplate<String, Object> redisTemplate;
	
	public FyTeacher loginTeacher(String tacher_phone, String password) {
	
		return mapper.loginTeacher(tacher_phone,password);
	}
	
	//老师登录
	/*public FyTeacher Tlogin(String tacher_phone, String password){
		
		return map;
	}*/

	public List<FyTeacher> selectAllTeacher() {
	
		return mapper.selectAllTeacher();
	}

	public FyTeacher selectByTeacherid(int teacher_id) {
		
		return mapper.selectByTeacherid(teacher_id);
	}

	public void updateTpass(FyTeacher Teacher) {
		mapper.updateTpass(Teacher);
		
		
	}

	public void updateTpho(FyTeacher Teacher) {
		mapper.updateTpho(Teacher);
		
	}

	public void Tfoget(String teacher_phone, String repassword) {
		mapper.Tfoget(teacher_phone, repassword);
		
	}

	public void TupdatePay(FyTeacher teacher) {
		mapper.TupdatePay(teacher);
		
	}

	//修改头像
	public void upload(MultipartFile file, Integer teacher_id) {
		String image = FTPUtil.setImage(file);
		FyTeacher teacher = mapper.selectByTeacherid(teacher_id);
		String rmPath = teacher.getTeacher_head_img();
		if (rmPath!=null) {
			
			FTPUtil.rmImage(rmPath); 
		}
		teacher.setTeacher_head_img(image);
		mapper.updateHead(teacher);
		
		
	}
	
	//(生产者)发送手机验证码
	public void sendCode(String teacher_phone) {
		
		String code = "";
		code=(int)((Math.random()*9+1)*100000)+"";
		Map<String, String> map = new HashMap<String, String>();
		map.put("mobile", teacher_phone);
		map.put("code", code);
		//将验证码存储到内存中一份
		redisTemplate.opsForValue().set(teacher_phone, code);
		//将验证码发送到消息队列
		amqpTemplate.convertAndSend("gyxExchange","gyx_queue",map);
	}
	//修改密码
	public String Tupdate(String code, String teacher_phone, String repassword) {
		// 从缓存中获取到验证码
		String code2 = (String) redisTemplate.opsForValue().get(teacher_phone);
		if(code2.equals(code)) { //用户输入的验证码与内存中的一致
			// 调用dao层的update方法，修改用户密码
			mapper.Tfoget(teacher_phone, repassword);
			redisTemplate.opsForValue().set(teacher_phone, 0);
			
			return "ok";
			
		}
		return "erro";
	}

	

	

}
